import 'package:baselib/base/base_responce.dart';
import 'package:baselib/util/common_apiservice.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_app/model/my_info_bean.dart';
import 'package:flutter_app/model/trade_bean.dart';
import 'package:flutter_app/util/urls.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';
import 'package:toast/toast.dart';
class MineController extends GetxController{

  static MineController get to => Get.find();
  var myinfoBean=MyInfoBean().obs;
  var tradeBean=TradeBean().obs;
  var records=List<Items>().obs;
  var page=1.obs;
  final int pageSize=10;


  ///获取用户基本信息
  Future<void> getInfos(BuildContext context) async{
    SmartDialog.showLoading();
    BaseResponce responce = await CommonApiService.requestGet(
      Get.context,
      url: Urls.me,
    );
    if(responce.code==200){
      MyInfoBean data=MyInfoBean.fromJson(responce.data);
      myinfoBean.value=data;
    }else{
      Toast.show(responce.message, context, duration: Toast.LENGTH_LONG, gravity:  Toast.TOP,backgroundColor:Colors.red);
    }
    SmartDialog.dismiss();
  }


  ///获取交易记录
  Future<void> getTradeInfos(BuildContext context) async{
    SmartDialog.showLoading();//v2/user/credit-list?page=1&page_size=20
    var body = {
      'page': page,
      'page_size': pageSize,
    };
    BaseResponce responce = await CommonApiService.requestGet(
      Get.context,
      url: Urls.credit_list,
      data: body
    );
    if(responce.code==200){
      TradeBean data=TradeBean.fromJson(responce.data);
      tradeBean.value=data;
      if(data.items==null||data.items.length==0){

      }else{
        records.addAll(data.items);
        page++;
      }

    }else{
      Toast.show(responce.message, context, duration: Toast.LENGTH_LONG, gravity:  Toast.TOP,backgroundColor:Colors.red);
    }
    SmartDialog.dismiss();
  }

}